<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2019/3/31
 * Time: 18:30
 */

namespace app\storage\controller;

use app\storage\model\ClientModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\Request;

class ClientController extends AdminBaseController
{
    private $clientModel;

    public function __construct(ClientModel $clientModel, Request $request = null)
    {
        parent::__construct($request);
        $this->clientModel = $clientModel;
    }

    public function index()
    {
        return $this->fetch('index');
    }

    public function randomStr()
    {
        $str = '0123456789abcdefghijklmnopqrstuvwxyz';
        $str2 = str_shuffle($str);
        $str3 = substr($str2, -4);
        return $str3;
    }

    public function add()
    {
        $client = $this->clientModel->select();
        $client['storage'] = Db::table('iwh_storage')->select();
        $this->assign('client', $client);
        return $this->fetch('add');
    }

    public function edit()
    {
        $id = input('id');
        $result = $this->clientModel->where('id', $id)->find();
        $assign['client'] = $result;
        $assign['storage'] = Db::table('iwh_storage')->select();
        $this->assign($assign);
        return $this->fetch('edit');
    }

    public function getClient()
    {
        $result = $this->clientModel->alias('c')
            ->join(['iwh_storage' => 's'], 'c.storage_uuid = s.uuid', 'left')
            ->field('s.name as storage_name,c.*')
            ->select();
        return json($result);
    }

    public function addClient()
    {
        $param = input();
        if ($param['code'] === "") {
            $param['code'] = $this->randomStr();
        }
        $where = [
            'code' => $param['code']
        ];
        $res = $this->clientModel->where($where)->find();
        while (count($res) >= 1) {
            $param['code'] = $this->randomStr();
            $res = $this->clientModel->where($where)->find();
        }
        $data = [
            'create_time' => get_current_time(),
            'is_online' => 0,
            'status' => 1,
            'uid' => create_uuid(),
            'storage_uuid' => $param['storage'],
            'code' => $param['code'],
            'name' => $param['name'],
            'remark' => $param['remark']
        ];
        $this->clientModel->insert($data);
        $this->redirect('/storage/Client/index');
    }

    public function deleteClient()
    {
        $id = input('id');
        Db::startTrans();
        $result = Db::table('iwh_client')->where('id', $id)->delete();
        if ($result !== false) {
            Db::commit();
            ajax_response(true, '删除成功');
        } else {
            Db::rollback();
            ajax_response(false, '删除失败');
        }
    }

    public function editClient()
    {
        $param = input();
        $data = [
            'name' => $param['name'],
            'storage_uuid' => $param['storage'],
            'remark' => $param['remark'],
            'status' => $param['status']
        ];
        $where = [
            'id' => $param['id']
        ];
        $this->clientModel->where($where)->update($data);
        $this->redirect('/storage/Client/index');
    }

    public function checkClientCode()
    {
        $id = input('id');
        $whereAnd = empty($id) ? '' : " and id != '$id'";
        //$where = sprintf("binary name = '%s' $whereAnd", input('name'));
        $where = sprintf("binary code = '%s' $whereAnd", input('code'));
        $repeat = $this->clientModel->where($where)->find();
        return boolval($repeat);
    }
}